//
//  UIBarButtonItem+Extension.swift
//  WeiBo
//
//  Created by Apple on 16/9/18.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  扩展UIBarButtonItem便利构造函数
extension UIBarButtonItem {
    //  使用默认值
    convenience init(title: String, imageName: String? = nil, target: Any?, action: Selector) {
        //  使用self调用其他构造函数
        self.init()
        
        let button = UIButton()
        //  添加点击事件
        button.addTarget(target, action: action, for: .touchUpInside)
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        if imageName != nil {
            button.setImage(UIImage(named: imageName!), for: .normal)
            button.setImage(UIImage(named: imageName! + "_highlighted"), for: .highlighted)
        }
        //  设置按钮的大小
        button.sizeToFit()
        
        
        customView = button
        
    }

}

